import java.util.Random;

public class Talia {
    private int[][] karty;
    private Random random;
    int karta = 0;
    int ranking[];

    protected Talia(){
        karty = new int[2][52];
        ranking = new int[52];
        this.random = new Random();
        for(int i = 0; i < 52; i++)
            karty[0][i] = i+1;
        ranking[0] = 1;
        ranking[1] = 1;
        ranking[2] = 1;
        ranking[3] = 1;
        ranking[4] = 12;
        ranking[5] = 12;
        ranking[6] = 12;
        ranking[7] = 12;
        ranking[8] = 2;
        ranking[9] = 3;
        ranking[10] = 4;
        ranking[11] = 5;
        ranking[12] = 6;
        ranking[13] = 7;
        ranking[14] = 8;
        ranking[15] = 9;
        ranking[16] = 10;
        ranking[17] = 11;
        ranking[18] = 11;
        ranking[19] = 11;
        ranking[20] = 11;
        ranking[21] = 13;
        ranking[22] = 13;
        ranking[23] = 13;
        ranking[24] = 13;
        ranking[25] = 2;
        ranking[26] = 3;
        ranking[27] = 4;
        ranking[28] = 5;
        ranking[29] = 6;
        ranking[30] = 7;
        ranking[31] = 8;
        ranking[32] = 9;
        ranking[33] = 10;
        ranking[34] = 2;
        ranking[35] = 3;
        ranking[36] = 4;
        ranking[37] = 5;
        ranking[38] = 6;
        ranking[39] = 7;
        ranking[40] = 8;
        ranking[41] = 9;
        ranking[42] = 10;
        ranking[43] = 2;
        ranking[44] = 3;
        ranking[45] = 4;
        ranking[46] = 5;
        ranking[47] = 6;
        ranking[48] = 7;
        ranking[49] = 8;
        ranking[50] = 9;
        ranking[51] = 10;

    }

    protected int daj_karte(){
        karta = karta+1;
        if(karta == 52) {
            tasuj();
            karta = karta%52;
        }
        while(karty[1][karta] == 1)
            karta = (karta+1)%52;
        karty[1][karta] = 1;
        return karty[0][karta];
    }

    protected void tasuj(){
        int l = 51;
        while (l > 0) {
            int losowa = liczba_losowa(l);
            if(losowa < l - 1){
                int a = karty[0][l-1];
                karty[0][l-1] = karty[0][losowa];
                karty[0][losowa] = a;
            }
            l--;
        }

    }

    int liczba_losowa(int zakres){
        return random.nextInt(zakres);
    }



    protected int[] wygral(int karty[][], int ilosc, int gracze[], int w_rundzie[][]){
        int t[][] = new int[ilosc][2];

        for (int i = 0; i < ilosc; i++){
            int suma = 0;
            int pary = 4;
            if(karty[i][0] == karty[i][1]) {
                pary--;
                karty[i][1] = -1;
            }
            if(karty[i][0] == karty[i][2]) {
                pary--;
                karty[i][2] = -2;
            }
            if(karty[i][0] == karty[i][3]) {
                pary--;
                karty[i][3] = -3;
            }
            if(karty[i][1] == karty[i][2]) {
                pary--;
                karty[i][2] = -4;
            }
            if(karty[i][1] == karty[i][3]) {
                pary--;
                karty[i][3] = -5;
            }
            if(karty[i][2] == karty[i][3]) {
                pary--;
                karty[i][3] = -5;
            }
            for(int j = 0; j < 4; j++){
                if (karty[i][j] > 0)
                    suma += karty[i][j];
            }
            t[i][0] = pary;
            t[i][1] = suma;

        }
        int max = -1;
        for (int i = 0; i < ilosc; i++){
            if(t[i][0] > max && gracze[i] == 0){
                if(w_rundzie[1][0] == 0)
                    max = t[i][0];
            }
        }

        int q = 1000000;
        int i[] = new int[2];
        for (int j = 0; j < ilosc; j++){
            if(max == t[j][0] && gracze[j] == 0) {
                if (q > t[j][1]) {
                    if(w_rundzie[1][0] == 0){

                        q = t[j][1];
                        i[0] = j;
                        i[1] = t[j][1];
                    }
                }

            }
        }

        return i;
    }

    protected void oddaj(int a){
        for(int i = 0; i < 52; i++){
            if(karty[0][i] == a){
                karty[1][i] = 0;
                break;
            }
        }
    }

    protected int[] co_mam(int kar[]){
        int t[] = new int[6];
        int suma = 0;
        int pary = 4;
        if(kar[0] == kar[1]) {
            pary--;
            kar[1] = -1;
        }
        if(kar[0] == kar[2]) {
            pary--;
            kar[2] = -2;
        }
        if(kar[0] == kar[3]) {
            pary--;
            kar[3] = -3;
        }
        if(kar[1] == kar[2]) {
            pary--;
            kar[2] = -4;
        }
        if(kar[1] == kar[3]) {
            pary--;
            kar[3] = -5;
        }
        if(kar[2] == kar[3]) {
            pary--;
            kar[3] = -5;
        }
        for(int j = 0; j < 4; j++){
            if (kar[j] > 0)
                suma += kar[j];
            t[j+2] = kar[j];
        }
        t[0] = pary;
        t[1] = suma;

        return t;
    }

}
